<template>
  <ElContainer style="height: 100%">
    <ElMain class="archive-info">
      <ElForm
        :inline="true"
        :model="formModel"
        :label-width="150"
        :show-message="false"
        ref="formRef"
      >
        <CardBox header="档案信息">
          <ElFormItem label="姓名">
            <ElInput v-model="formModel.a00_03_05" />
          </ElFormItem>
          <ElFormItem label="健康档案号">
            <ElInput v-model="formModel.organizeCode" readonly />
          </ElFormItem>
          <ElFormItem>
            <ElInput v-model="formModel.a00_03_04" readonly />
          </ElFormItem>
          <ElFormItem label="社区(村)">
            <el-cascader
              ref="communityCascaderRef"
              :show-all-levels="false"
              :options="communityOptions"
              @change="handleCommunityChange"
              :props="{ label: 'u_name', value: 'u_name', emitPath: false }"
              filterable
              v-model="formModel.a00_03_97"
            />
          </ElFormItem>
          <ElFormItem label="详细地址" required prop="a00_03_99">
            <ElInput v-model="formModel.a00_03_99" />
          </ElFormItem>
          <ElFormItem label="关联家庭卡号">
            <ElSelect
              :filterable="true"
              :remote="true"
              v-model="formModel.family_id"
              :remote-method="familyRemoteMethod"
              @change="familyChange"
            >
              <ElOption
                v-for="item in familyList"
                :label="item.a00_02_13"
                :value="item.id"
                :key="item.id"
              />
            </ElSelect>
          </ElFormItem>
        </CardBox>
        <CardBox header="个人基本情况">
          <ElFormItem label="性别" prop="a00_03_07">
            <ElRadioGroup v-model="formModel.a00_03_07">
              <ElRadio
                v-for="(value, key) in formSource.a00_03_07"
                :value="key"
                :key="key"
                :label="key"
                >{{ value }}
              </ElRadio>
            </ElRadioGroup>
          </ElFormItem>

          <ElFormItem label="户主关系" prop="a00_03_08">
            <ElSelect v-model="formModel.a00_03_08">
              <ElOption
                v-for="(value, key) in formSource.a00_03_08"
                :label="value"
                :value="key"
                :key="key"
                >{{ value }}
              </ElOption>
            </ElSelect>
          </ElFormItem>
          <br />
          <ElFormItem label="身份证号" prop="a00_03_10">
            <ElInput v-model="formModel.a00_03_10" />
          </ElFormItem>
          <ElFormItem label="出生日期" prop="a00_03_09">
            <ElDatePicker
              v-model="formModel.a00_03_09"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </ElFormItem>
          <br />
          <ElFormItem label="本人电话" prop="a00_03_12">
            <ElInput v-model="formModel.a00_03_12" />
          </ElFormItem>

          <ElFormItem label="工作单位" prop="a00_03_11">
            <ElInput v-model="formModel.a00_03_11" />
          </ElFormItem>
          <br />

          <ElFormItem label="联系人姓名(一)" prop="a00_03_13">
            <ElInput v-model="formModel.a00_03_13" />
          </ElFormItem>

          <ElFormItem label="关系" prop="a00_03_132">
            <ElSelect v-model="formModel.a00_03_132">
              <ElOption
                v-for="(value, key) in formSource.a00_03_132"
                :value="key"
                :label="value"
                :key="key"
                >{{ value }}
              </ElOption>
            </ElSelect>
          </ElFormItem>

          <ElFormItem label="联系人电话（一）" prop="a00_03_14">
            <ElInput v-model="formModel.a00_03_14" />
          </ElFormItem>
          <br />

          <ElFormItem label="联系人姓名(二)" prop="a00_03_133">
            <ElInput v-model="formModel.a00_03_133" />
          </ElFormItem>

          <ElFormItem label="关系" prop="a00_03_135">
            <ElSelect v-model="formModel.a00_03_135">
              <ElOption
                v-for="(value, key) in formSource.a00_03_132"
                :value="key"
                :label="value"
                :key="key"
                >{{ value }}
              </ElOption>
            </ElSelect>
          </ElFormItem>

          <ElFormItem label="联系人电话（二）" prop="a00_03_134">
            <ElInput v-model="formModel.a00_03_134" />
          </ElFormItem>
          <br />
          <ElFormItem label="常住类型" prop="a00_03_15">
            <ElSelect v-model="formModel.a00_03_15">
              <ElOption
                v-for="(value, key) in formSource.a00_03_15"
                :value="key"
                :key="key"
                :label="value"
              >
                {{ value }}
              </ElOption>
            </ElSelect>
          </ElFormItem>

          <ElFormItem label="民族" prop="a00_03_16">
            <ElSelect v-model="formModel.a00_03_16">
              <ElOption
                v-for="(value, key) in formSource.a00_03_16"
                :value="key"
                :key="key"
                :label="value"
                >{{ value }}
              </ElOption>
            </ElSelect>
          </ElFormItem>

          <ElFormItem label="现住址" prop="a00_03_116">
            <ElInput v-model="formModel.a00_03_116" />
          </ElFormItem>
          <br />
          <ElFormItem label="血型" prop="a00_03_18">
            <ElRadioGroup v-model="formModel.a00_03_18">
              <ElRadio
                v-for="(value, key) in formSource.a00_03_18"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>

          <ElFormItem label="RH阴性" prop="a00_03_19">
            <ElRadioGroup v-model="formModel.a00_03_19">
              <ElRadio
                v-for="(value, key) in formSource.a00_03_19"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <br />
          <ElFormItem label="文化程度" prop="a00_03_20">
            <ElRadioGroup v-model="formModel.a00_03_20">
              <ElRadio
                v-for="(value, key) in formSource.a00_03_20"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <br />
          <ElFormItem label="职业" prop="a00_03_21">
            <ElSelect v-model="formModel.a00_03_21">
              <ElOption
                v-for="(value, key) in formSource.a00_03_21"
                :value="key"
                :label="value"
                :key="key"
              />
            </ElSelect>
          </ElFormItem>

          <ElFormItem label="婚姻状况" prop="a00_03_23">
            <ElRadioGroup v-model="formModel.a00_03_23">
              <ElRadio
                v-for="(value, key) in formSource.a00_03_23"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <br />
          <ElFormItem label="医保卡号" prop="a00_03_54">
            <ElInput
              v-model="formModel.a00_03_54"
              :maxlength="18"
              show-word-limit
            />
          </ElFormItem>

          <ElFormItem label="医疗费用支付方式" prop="a00_03_24">
            <ElCheckboxGroup
              v-model="formModel.a00_03_24"
              @change="handleArrayExcludeClear($event, '8', ['a00_03_25'])"
            >
              <ElCheckbox
                v-for="(value, key) in formSource.a00_03_24"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_24', '1', [
                    'a00_03_25',
                  ])
                "
              >
                {{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
          </ElFormItem>
          <ElFormItem
            label=""
            prop="a00_03_25"
            :required="formModel.a00_03_24?.includes('8')"
          >
            <ElInput
              v-model="formModel.a00_03_25"
              :disabled="!formModel.a00_03_24?.includes('8')"
            />
          </ElFormItem>
          <br />
          <ElFormItem label="药物过敏史" prop="a00_03_26">
            <ElCheckboxGroup
              v-model="formModel.a00_03_26"
              @change="handleArrayExcludeClear($event, '5', ['a00_03_27'])"
            >
              <ElCheckbox
                v-for="(value, key) in formSource.a00_03_26"
                :value="key"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_26', '1', [
                    'a00_03_27',
                  ])
                "
              >
                {{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
          </ElFormItem>
          <ElFormItem
            label=""
            prop="a00_03_27"
            :required="formModel.a00_03_26?.includes('5')"
          >
            <ElInput
              v-model="formModel.a00_03_27"
              :disabled="!formModel.a00_03_26?.includes('5')"
            />
          </ElFormItem>
          <ElFormItem label="暴露史" prop="a00_03_69">
            <ElCheckboxGroup v-model="formModel.a00_03_69">
              <ElCheckbox
                v-for="(value, key) in formSource.a00_03_69"
                :value="key"
                :label="key"
                :key="key"
                @change="handleCheckboxIncludeClearOther(key, 'a00_03_69', '1')"
              >
                {{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
          </ElFormItem>
        </CardBox>
        <CardBox header="既往史">
          <ElFormItem label="疾病" prop="a00_031_04" style="width: 100%">
            <ElTable
              :data="formModel.disease"
              style="width: 100%"
              border
              stripe
            >
              <ElTableColumn
                size="small"
                type="selection"
                align="center"
                label="选择"
                width="40"
              >
                <template #default="scope">
                  <el-checkbox
                    v-model="scope.row.state"
                    @change="diseaseSelectChange($event, scope.row)"
                  />
                </template>
              </ElTableColumn>
              <ElTableColumn
                label="名称"
                align="center"
                prop="name"
                width="200"
              />
              <ElTableColumn label="时间" align="center" width="300">
                <template #default="scope">
                  <ElFormItem
                    :prop="`disease.${scope.$index}.date`"
                    required
                    v-if="scope.row.state && scope.row.hasDate !== false"
                  >
                    <ElDatePicker
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      v-model="scope.row.date"
                    />
                  </ElFormItem>
                </template>
              </ElTableColumn>
              <ElTableColumn
                label="具体名称"
                align="center"
                width="400"
                required
              >
                <template #default="scope">
                  <ElFormItem
                    :prop="`disease.${scope.$index}.aliasName`"
                    required
                    v-if="scope.row.state && scope.row.hasAlias"
                  >
                    <ElInput v-model="scope.row.aliasName" />
                  </ElFormItem>
                </template>
              </ElTableColumn>
            </ElTable>
          </ElFormItem>
          <br />
          <ElRow>
            <ElCol :span="6">
              <ElFormItem label="手术">
                <ElRadioGroup
                  v-model="formModel.a00_031_07"
                  @change="handleTableIfChange($event, 'surgery')"
                >
                  <ElRadio label="1">无</ElRadio>
                  <ElRadio label="2">有</ElRadio>
                </ElRadioGroup>
              </ElFormItem>
            </ElCol>
            <ElCol :span="18">
              <ElFormItem v-if="formModel.a00_031_07 === '2'">
                <ElButton @click="handleTableRowAdd('surgery')"> 新增</ElButton>
                <ElTable :data="formModel.surgery" :fit="true">
                  <ElTableColumn label="序号" type="index" min-width="40px" />
                  <ElTableColumn label="名称" align="center">
                    <template #default="scope">
                      <ElFormItem
                        required
                        :prop="`surgery.${scope.$index}.name`"
                      >
                        <ElInput v-model="scope.row.name" />
                      </ElFormItem>
                    </template>
                  </ElTableColumn>
                  <ElTableColumn label="时间" align="center">
                    <template #default="scope">
                      <ElFormItem
                        required
                        :prop="`surgery.${scope.$index}.date`"
                      >
                        <ElDatePicker
                          format="YYYY-MM-DD"
                          value-format="YYYY-MM-DD"
                          v-model="scope.row.date"
                        />
                      </ElFormItem>
                    </template>
                  </ElTableColumn>
                  <ElTableColumn label="操作" align="center">
                    <template #default="scope">
                      <ElButton
                        type="danger"
                        @click="handleTableRowDel(scope, 'surgery')"
                        >删除</ElButton
                      >
                    </template>
                  </ElTableColumn>
                </ElTable>
              </ElFormItem>
            </ElCol>
          </ElRow>

          <br />
          <ElRow>
            <ElCol :span="6">
              <ElFormItem label="外伤">
                <ElRadioGroup
                  v-model="formModel.a00_031_10"
                  @change="handleTableIfChange($event, 'trauma')"
                >
                  <ElRadio label="1">无</ElRadio>
                  <ElRadio label="2">有</ElRadio>
                </ElRadioGroup>
              </ElFormItem>
            </ElCol>
            <ElCol :span="18">
              <ElFormItem v-if="formModel.a00_031_10 === '2'">
                <ElButton @click="handleTableRowAdd('trauma')"> 新增</ElButton>
                <ElTable :data="formModel.trauma" :fit="true">
                  <ElTableColumn label="序号" type="index" min-width="40px" />
                  <ElTableColumn label="名称" align="center">
                    <template #default="scope">
                      <ElFormItem
                        required
                        :prop="`trauma.${scope.$index}.name`"
                      >
                        <ElInput v-model="scope.row.name" />
                      </ElFormItem>
                    </template>
                  </ElTableColumn>
                  <ElTableColumn label="时间" align="center">
                    <template #default="scope">
                      <ElFormItem
                        required
                        :prop="`trauma.${scope.$index}.date`"
                      >
                        <ElDatePicker
                          format="YYYY-MM-DD"
                          value-format="YYYY-MM-DD"
                          v-model="scope.row.date"
                        />
                      </ElFormItem>
                    </template>
                  </ElTableColumn>
                  <ElTableColumn label="操作" align="center">
                    <template #default="scope">
                      <ElButton
                        type="danger"
                        @click="handleTableRowDel(scope, 'trauma')"
                        >删除</ElButton
                      >
                    </template>
                  </ElTableColumn>
                </ElTable>
              </ElFormItem>
            </ElCol>
          </ElRow>
          <br />
          <ElRow>
            <ElCol :span="6">
              <ElFormItem label="输血">
                <ElRadioGroup
                  v-model="formModel.a00_031_13"
                  @change="handleTableIfChange($event, 'transfuse')"
                >
                  <ElRadio label="1">无</ElRadio>
                  <ElRadio label="2">有</ElRadio>
                </ElRadioGroup>
              </ElFormItem>
            </ElCol>
            <ElCol :span="18">
              <ElFormItem v-if="formModel.a00_031_13 === '2'">
                <ElButton @click="handleTableRowAdd('transfuse')">
                  新增</ElButton
                >
                <ElTable :data="formModel.transfuse" :fit="true">
                  <ElTableColumn label="序号" type="index" min-width="40px" />
                  <ElTableColumn label="名称" align="center">
                    <template #default="scope">
                      <ElFormItem
                        required
                        :prop="`transfuse.${scope.$index}.name`"
                      >
                        <ElInput v-model="scope.row.name" />
                      </ElFormItem>
                    </template>
                  </ElTableColumn>
                  <ElTableColumn label="时间" align="center">
                    <template #default="scope">
                      <ElFormItem
                        required
                        :prop="`transfuse.${scope.$index}.date`"
                      >
                        <ElDatePicker
                          format="YYYY-MM-DD"
                          value-format="YYYY-MM-DD"
                          v-model="scope.row.date"
                        />
                      </ElFormItem>
                    </template>
                  </ElTableColumn>
                  <ElTableColumn label="操作" align="center">
                    <template #default="scope">
                      <ElButton
                        type="danger"
                        @click="handleTableRowDel(scope, 'transfuse')"
                        >删除</ElButton
                      >
                    </template>
                  </ElTableColumn>
                </ElTable>
              </ElFormItem>
            </ElCol>
          </ElRow>
        </CardBox>
        <CardBox header="家族史">
          <ElFormItem label="父亲">
            <ElCheckboxGroup
              v-model="formModel.a00_03_28"
              @change="handleArrayExcludeClear($event, '12', ['a00_03_29'])"
            >
              <ElCheckbox
                v-for="(value, key) in formSource.jzs"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_28', '1', [
                    'a00_03_29',
                  ])
                "
                >{{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
            <ElFormItem
              label=""
              :required="formModel.a00_03_28?.includes('12')"
            >
              <ElInput
                v-model="formModel.a00_03_29"
                :disabled="!formModel.a00_03_28?.includes('12')"
              />
            </ElFormItem>
          </ElFormItem>
          <ElFormItem label="母亲">
            <ElCheckboxGroup
              v-model="formModel.a00_03_30"
              @change="handleArrayExcludeClear($event, '12', ['a00_03_31'])"
            >
              <ElCheckbox
                v-for="(value, key) in formSource.jzs"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_30', '1', [
                    'a00_03_31',
                  ])
                "
                >{{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
            <ElFormItem
              label=""
              :required="formModel.a00_03_30?.includes('12')"
            >
              <ElInput
                v-model="formModel.a00_03_31"
                :disabled="!formModel.a00_03_30?.includes('12')"
              />
            </ElFormItem>
          </ElFormItem>
          <ElFormItem label="兄弟姐妹">
            <ElCheckboxGroup
              v-model="formModel.a00_03_32"
              @change="handleArrayExcludeClear($event, '12', ['a00_03_33'])"
            >
              <ElCheckbox
                v-for="(value, key) in formSource.jzs"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_32', '1', [
                    'a00_03_33',
                  ])
                "
                >{{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
            <ElFormItem
              label=""
              :required="formModel.a00_03_32?.includes('12')"
            >
              <ElInput
                v-model="formModel.a00_03_33"
                :disabled="!formModel.a00_03_32?.includes('12')"
              />
            </ElFormItem>
          </ElFormItem>
          <ElFormItem label="子女">
            <ElCheckboxGroup
              v-model="formModel.a00_03_34"
              @change="handleArrayExcludeClear($event, '12', ['a00_03_35'])"
            >
              <ElCheckbox
                v-for="(value, key) in formSource.jzs"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_34', '1', [
                    'a00_03_35',
                  ])
                "
                >{{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
            <ElFormItem
              label=""
              :required="formModel.a00_03_34?.includes('12')"
            >
              <ElInput
                v-model="formModel.a00_03_35"
                :disabled="!formModel.a00_03_34?.includes('12')"
              />
            </ElFormItem>
          </ElFormItem>
        </CardBox>
        <CardBox header="遗传病史及其他情况">
          <ElFormItem label="遗传病史">
            <ElRadioGroup
              v-model="formModel.a00_03_36"
              @change="handleStringExcludeClear($event, '2', ['a00_03_37'])"
            >
              <ElRadio label="1">无</ElRadio>
              <ElRadio label="2">有</ElRadio>
            </ElRadioGroup>
            <ElFormItem label="" :required="formModel.a00_03_36 === '2'">
              <ElInput
                v-model="formModel.a00_03_37"
                :disabled="formModel.a00_03_36 !== '2'"
              />
            </ElFormItem>
          </ElFormItem>
          <ElFormItem label="残疾情况">
            <ElCheckboxGroup
              v-model="formModel.a00_03_38"
              @change="handleArrayExcludeClear($event, '8', ['a00_03_39'])"
            >
              <ElCheckbox
                v-for="(value, key) in formSource.a00_03_38"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_38', '1', [
                    'a00_03_39',
                  ])
                "
                >{{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
            <ElFormItem
              label=" "
              :label-width="6"
              :required="formModel.a00_03_38?.includes('8')"
            >
              <ElInput
                v-model="formModel.a00_03_39"
                :disabled="!formModel.a00_03_38?.includes('8')"
              />
            </ElFormItem>
          </ElFormItem>
          <ElFormItem label="独居情况">
            <ElRadioGroup v-model="formModel.a00_03_65">
              <ElRadio label="1">是</ElRadio>
              <ElRadio label="2">否</ElRadio>
            </ElRadioGroup>
          </ElFormItem>
          <ElFormItem label="困难人群">
            <ElCheckboxGroup
              v-model="formModel.a00_03_121"
              @change="
                handleArrayExcludeClear($event, '6', [
                  'a00_03_138',
                  'a00_03_143',
                  'a00_03_144',
                ])
              "
            >
              <ElCheckbox
                v-for="(value, key) in formSource.a00_03_121"
                :label="key"
                :key="key"
                @change="
                  handleCheckboxIncludeClearOther(key, 'a00_03_121', '1', [
                    'a00_03_138',
                    'a00_03_143',
                    'a00_03_144',
                  ])
                "
                >{{ value }}
              </ElCheckbox>
            </ElCheckboxGroup>
            <ElFormItem
              label="建档立卡卡号"
              :label-width="120"
              :required="formModel.a00_03_121?.includes('6')"
            >
              <ElInput
                v-model="formModel.a00_03_138"
                :disabled="!formModel.a00_03_121?.includes('6')"
              />
            </ElFormItem>
            <ElFormItem
              label="是否脱贫"
              :label-width="70"
              :required="formModel.a00_03_121?.includes('6')"
            >
              <ElRadioGroup
                v-model="formModel.a00_03_143"
                :disabled="!formModel.a00_03_121?.includes('6')"
              >
                <ElRadio label="0">是</ElRadio>
                <ElRadio label="1">否</ElRadio>
              </ElRadioGroup>
            </ElFormItem>
            <ElFormItem
              label="脱贫日期"
              :label-width="70"
              :required="formModel.a00_03_143 === '0'"
            >
              <ElDatePicker
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                v-model="formModel.a00_03_144"
                :disabled="formModel.a00_03_143 !== '0'"
              />
            </ElFormItem>
          </ElFormItem>
          <ElFormItem label="居住形式">
            <ElRadioGroup v-model="formModel.a00_03_120">
              <ElRadio
                v-for="(value, key) in formSource.a00_03_120"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <ElFormItem label="档案核实">
            <ElRadioGroup v-model="formModel.a00_03_136">
              <ElRadio label="1">已核实</ElRadio>
              <ElRadio label="">未核实</ElRadio>
            </ElRadioGroup>
          </ElFormItem>
        </CardBox>
        <CardBox header="生活环境">
          <ElFormItem label="厨房排风设施">
            <ElRadioGroup v-model="formModel.a00_03_111">
              <ElRadio
                v-for="(value, key) in formSource.a00_02_111"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <br />
          <ElFormItem label="燃料类型" prop="a00_03_112">
            <ElRadioGroup v-model="formModel.a00_03_112">
              <ElRadio
                v-for="(value, key) in formSource.a00_02_112"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <br />
          <ElFormItem label="饮水" prop="a00_03_113">
            <ElRadioGroup v-model="formModel.a00_03_113">
              <ElRadio
                v-for="(value, key) in formSource.a00_02_113"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <br />
          <ElFormItem label="厕所" prop="a00_03_114">
            <ElRadioGroup v-model="formModel.a00_03_114">
              <ElRadio
                v-for="(value, key) in formSource.a00_02_114"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
          <br />
          <ElFormItem label="禽畜栏" prop="a00_03_115">
            <ElRadioGroup v-model="formModel.a00_03_115">
              <ElRadio
                v-for="(value, key) in formSource.a00_02_115"
                :label="key"
                :key="key"
                >{{ value }}</ElRadio
              >
            </ElRadioGroup>
          </ElFormItem>
        </CardBox>
        <!--        <CardBox header="家庭地址">-->
        <!--          <ElFormItem label="省">-->
        <!--            <ElInput v-model="formModel.a00_03_93" readonly />-->
        <!--          </ElFormItem>-->
        <!--          <ElFormItem label="市">-->
        <!--            <ElInput v-model="formModel.a00_03_94" readonly />-->
        <!--          </ElFormItem>-->
        <!--          <ElFormItem label="区县">-->
        <!--            <ElInput v-model="formModel.a00_03_95" readonly />-->
        <!--          </ElFormItem>-->
        <!--          <ElFormItem label="社区中心(乡)">-->
        <!--            <ElInput v-model="formModel.a00_03_96" readonly />-->
        <!--          </ElFormItem>-->
        <!--        </CardBox>-->
        <CardBox header="服务机构">
          <ElFormItem label="责任医生">
            <ElSelect v-model="formModel.a00_03_51">
              <ElOption
                v-for="item in doctorsList"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              />
            </ElSelect>
            <!--            医生名称-->
            <input type="hidden" name="a00_03_91" />
          </ElFormItem>
          <ElFormItem label="建档日期">
            <ElDatePicker
              v-model="formModel.a00_03_53"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </ElFormItem>
          <ElFormItem label="服务机构">
            <ElInput v-model="formModel.a00_03_92" readonly />
          </ElFormItem>
          <ElFormItem label="建档人">
            <!--            医生名称-->
            <ElSelect v-model="formModel.a00_03_52">
              <ElOption
                v-for="item in doctorsList"
                :key="item.id"
                :value="item.name"
                :label="item.name"
              />
            </ElSelect>
          </ElFormItem>
        </CardBox>
      </ElForm>
    </ElMain>
  </ElContainer>
</template>
<script lang="ts" setup>
import { reactive, ref, toRaw, onMounted } from "vue";
import { a00_03, a00_02 } from "@/dict";
import buildFormUtils from "../HealthArchives/formUtils";
import { IA00_03, IDisease } from "@/model";
import { ElForm, ElMessage } from "element-plus";
import { useCommunitySelect, useDoctorSelect } from "../useCommonApi";
import {
  getFamilyArchivesList,
  getProfilesInfoByCardId,
  saveProfiles,
} from "@/api/archives";
import useMedicalUser from "@/hooks/useMedicalUser";
import { modelUtils } from "@/model";
import { cloneDeep, pickBy } from "lodash-es";

const { medicalUser } = useMedicalUser();

const { doctorsList } = useDoctorSelect();
const { communityOptions } = useCommunitySelect();

const props = defineProps<{
  data?: IA00_03;
}>();

const communityCascaderRef = ref();

//获取家庭列表
const handleCommunityChange = (val) => {
  if (val) {
    const checkedNodes = communityCascaderRef.value.getCheckedNodes(); // 获取当前点击的节点
    console.log(checkedNodes[0].data, "checkedNodes[0].data");

    formModel.value.a00_03_70 = checkedNodes[0].data.u_id;
    getFamilyList();
  }
};
const familyList = reactive<Array<any>>([]);
const getFamilyList = async (query?: string) => {
  const params = {
    //社区
    a00_02_97: formModel.value.a00_03_97,
    //  详细地址
    a00_02_13: query,
  };
  const getParams = pickBy({ ...params });
  const data: any = await getFamilyArchivesList(getParams);
  Object.assign(familyList, data.data);
};
const familyRemoteMethod = (query) => {
  if (query) {
    getFamilyList(query);
  }
};
onMounted(() => {
  //编辑时，默认请求一次
  if (formModel.value.a00_03_97 && formModel.value.a00_03_13) {
    familyRemoteMethod(formModel.value.a00_03_13);
  }
});
const familyChange = (val) => {
  const data = familyList.find((item) => item.id === val);
  formModel.value.a00_03_99 = data.a00_02_13;
};
const createPersonalArchive = (): IA00_03 => {
  const personalArchives = modelUtils.createPersonalArchive();
  personalArchives.a00_03_10 = medicalUser.value?.certNumber || "";
  personalArchives.a00_03_05 = medicalUser.value?.partyName || "";
  personalArchives.a00_03_07 =
    medicalUser.value?.gender === 0
      ? "2"
      : medicalUser.value?.gender === 1
      ? "1"
      : "";
  return personalArchives;
};
//初始化
const formModel = ref(createPersonalArchive());
onMounted(async () => {
  if (props.data) {
    formModel.value = cloneDeep(props.data);
  } else {
    //  TODO:通过cardId 获取formModel
    try {
      const data = await getProfilesInfoByCardId(medicalUser.value?.certNumber);
      formModel.value = data;
    } catch (e) {
      console.log(e);
    }
  }
});

const formRef = ref<InstanceType<typeof ElForm>>();

const handleTableRowAdd = (type) => {
  formModel.value[type].push({});
};
const handleTableRowDel = (scope, type) => {
  // if (formModel.value[type].length <= 1) {
  //   ElMessage({
  //     message: "至少保留一条",
  //     type: "error",
  //   });
  //   return;
  // }
  formModel.value[type].splice(scope.$index, 1);
};
const handleTableIfChange = (value, val) => {
  if (value === "1") {
    formModel.value[val] = undefined;
  } else if (value === "2") {
    formModel.value[val] = [{}];
  }
};

const {
  handleCheckboxIncludeClearOther,
  handleArrayExcludeClear,
  handleStringExcludeClear,
} = buildFormUtils(formModel);

const createDisease = (): Array<IDisease> => {
  return [
    {
      code: "1",
      name: "无",
      state: true,
      date: "",
      hasDate: false,
    },
    {
      code: "2",
      name: "高血压",
      state: false,
      date: "",
    },
    {
      code: "3",
      name: "糖尿病",
      state: false,
      date: "",
    },
    {
      code: "4",
      name: "冠心病",
      state: false,
      date: "",
    },
    {
      code: "5",
      name: "慢性阻塞性肺疾病",
      state: false,
      date: "",
    },
    {
      code: "6",
      name: "恶性肿瘤",
      state: false,
      date: "",
      aliasName: "",
      hasAlias: true,
    },
    {
      code: "7",
      name: "脑卒中",
      state: false,
      date: "",
      aliasName: "",
    },
    {
      code: "8",
      name: "严重精神障碍",
      state: false,
      date: "",
      aliasName: "",
    },
    {
      code: "9",
      name: "结核病",
      state: false,
      date: "",
      aliasName: "",
    },
    {
      code: "10",
      name: "肝炎",
      state: false,
      date: "",
      aliasName: "",
    },
    {
      code: "11",
      name: "其他法定传染病",
      state: false,
      date: "",
      aliasName: "",
    },
    {
      code: "12",
      name: "职业病",
      state: false,
      date: "",
      aliasName: "",
      hasAlias: true,
    },
    {
      code: "13",
      name: "其他",
      state: false,
      date: "",
      aliasName: "",
      hasAlias: true,
    },
  ];
};
const diseaseSelectChange = (value, row) => {
  if (value) {
    if (row.code === "1") {
      formModel.value.disease = [];
    } else {
      try {
        const data = formModel.value.disease?.find((item) => item.code === "1");
        if (data) {
          data.state = false;
        }
      } catch (e) {
        console.log(e);
      }
    }
  }
};

const save = async () => {
  const data = await formRef.value?.validate();
  if (data) {
    return await saveProfiles(toRaw(formModel.value));
  }
};
defineExpose({ save });

const formSource = reactive({ ...a00_02, ...a00_03 });
</script>
<style lang="scss" scoped>
:deep(.el-table__header-wrapper .el-table-column--selection) {
  text-indent: -999999px;
}

.input-number-width {
  :deep(.el-input__inner) {
    width: 80px;
  }
}

.archive-info {
  input[type="number"] {
    width: 100px;
  }

  .el-tabs__content {
    overflow: auto;
    height: calc(100vh - 340px);
  }
}

:deep(.el-table__row .el-form-item) {
  margin-bottom: 0;
}
</style>
